package com.ielts.fragment;

import vn.com.baseapplication.BaseFragment;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

import com.ielts.activity.R;
import com.ielts.services.IeltsUrl;

public class FanpageFragment extends BaseFragment implements OnClickListener{
	WebView wvFanpage;
	View loading;
	ImageButton btnRefresh;
	Animation rotateRefresh;
	
	final String urlFanpage = IeltsUrl.URL_FANPAGE;
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.fragment_fanpage, null);
		wvFanpage = (WebView) layout.findViewById(R.id.webview_help);
		loading = layout.findViewById(R.id.loading);
		btnRefresh = (ImageButton)layout.findViewById(R.id.btn_refresh);
		
		rotateRefresh = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_refresh);
		
		btnRefresh.setOnClickListener(this);
		
		WebSettings settings = wvFanpage.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setDomStorageEnabled(true);
				
		wvFanpage.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				loading.setVisibility(View.VISIBLE);
				rotateRefresh.cancel();
				btnRefresh.startAnimation(rotateRefresh);
			}
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				loading.setVisibility(View.GONE);
				rotateRefresh.cancel();
			}
			
	 
	        @Override
	        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
	            super.onReceivedSslError(view, handler, error);
	 
	            // this will ignore the Ssl error and will go forward to your site
	            handler.proceed();
	        }
		});
		wvFanpage.loadUrl(urlFanpage);
		return layout;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_refresh:
			wvFanpage.loadUrl(urlFanpage);
			break;

		default:
			break;
		}
	}
	
}
